package com.bot.rf.config;

import com.bot.rf.util.MyMail;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: report-form-demo
 * @description: 用户静态配置参数，数据库、邮箱等
 * @author: zyy
 * @create: 2021-02-24 14:20
 **/
public class UserConfig {

    //--------------------------- 系统配置参数 start ---------------------------
    /**
     * 桌面路径  例如：C:\Users\Dell\Desktop
     */
    public static final String DESKTOP_PATH = FileSystemView.getFileSystemView().getHomeDirectory().getPath();
    public static final String PROJECT_PATH = DESKTOP_PATH + "\\Tooker";
    public static final String REPORT_PATH = PROJECT_PATH + "\\Report";
    public static final String REPORT_END_PATH = REPORT_PATH + "\\REPORT_END";

    /**
     * 配置文件的路径
     */
    private static final String XML_PATH = REPORT_PATH + "\\config.xml";
    //--------------------------- 系统配置参数 end -----------------------------

    //--------------------------- 调试配置参数 start ---------------------------

    public static boolean IS_FIRST = false;

    public static boolean UPDATE_DATABASE = false;

    public static boolean HTTP_DATABASE = false;

    public static boolean UPLOAD_TO_SERVER = false;


    public static boolean CHECK_LOSS = false;

    //--------------------------- 调试配置参数 end -----------------------------

    //--------------------------- 用户配置参数 start ---------------------------
    /**
     * xml的Dom树
     */
    private static Document configDom;

    /**
     * 数据库配置
     */
    public static String USER_DATABASE_HOST;
    public static int USER_DATABASE_PORT;
    public static String USER_DATABASE_NAME;
    public static String USER_MAIN_TABLE_NAME;
    public static String USER_UPDATE_TABLE_NAME;
    public static String USER_DATABASE_USERNAME;
    public static String USER_DATABASE_PASSWORD;
    /**
     * oracle数据库专用 sid
     */
    public static String USER_DATABASE_SID;

    /**
     * 邮件配置
     */
    public static MyMail MAIL;
    public static boolean ENABLE_EMAIL;
    public static String EMAIL_HOST;
    public static String EMAIL_PORT;
    public static String EMAIL_FROM_ACCOUNT;
    public static String EMAIL_FROM_PASSWORD;
    public static String EMAIL_FROM_NAME;
    public static Map<String, String> EMAIL_TO_ACCOUNT;

    public static boolean AB_ENABLE;
    public static boolean NEAR_ENABLE;
    public static boolean RESET_DATABASE;
//
//    /**
//     * ftp配置
//     */
//    public static boolean FTP_OPEN;
//    public static String FTP_URL;
//    public static String FTP_ACCOUNT;
//    public static String FTP_PASSWORD;
//    public static Integer FTP_PORT;

    /**
     * socket
     */
//    public static boolean SOCKET_OPEN;
//    public static String SOCKET_IP;
//    public static String SOCKET_PORT;

    //--------------------------- 用户配置参数 end -----------------------------

    /**
     * 重写无参构造函数
     */
    public UserConfig(){
        try {
            configDom = new SAXReader().read(new File(XML_PATH));
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element config = configDom.getRootElement();
        Element bookInfo = config.element("bookInfo");

        // database config
        Element databaseConfig = bookInfo.element("databaseConfig");
        USER_DATABASE_HOST = databaseConfig.elementText("host");
        USER_DATABASE_PORT = Integer.parseInt(databaseConfig.elementText("portNo"));
        USER_DATABASE_NAME = databaseConfig.elementText("databaseName");
        USER_MAIN_TABLE_NAME = databaseConfig.elementText("tableMainName");
        USER_UPDATE_TABLE_NAME = databaseConfig.elementText("tableBorrowName");
        USER_DATABASE_USERNAME = databaseConfig.elementText("username");
        USER_DATABASE_PASSWORD = databaseConfig.elementText("password");
        if (databaseConfig.elementText("sid") != null){
            USER_DATABASE_SID = databaseConfig.elementText("sid");
        }
        IS_FIRST = config.elementText("isFirst").equals("true");
        UPDATE_DATABASE = config.elementText("updateDatabase").equals("true");
        HTTP_DATABASE = config.elementText("http").equals("true");
        UPLOAD_TO_SERVER = config.elementText("upload-to-server").equals("true");
        AB_ENABLE = config.elementText("ab-enable").equals("true");
        NEAR_ENABLE = config.elementText("near-enable").equals("true");
        RESET_DATABASE = config.elementText("resetDatabase").equals("true");
        CHECK_LOSS = config.elementText("enableCheckLoss").equals("true");
        // 修改第一次标志
        if (IS_FIRST) {
            config.element("isFirst").setText("false");
        }

        // mail config
        Element configMail = config.element("mailConfig");
        ENABLE_EMAIL = configMail.elementText("enableMail").equals("true");
        EMAIL_HOST = configMail.elementText("emailHost");
        EMAIL_PORT = configMail.elementText("emailPort");
        Element configFrom = configMail.element("from");
        Element configTo = configMail.element("to");
        EMAIL_FROM_ACCOUNT = configFrom.elementText("account");
        EMAIL_FROM_PASSWORD = configFrom.elementText("password");
        EMAIL_FROM_NAME = configFrom.element("account").attributeValue("name");
        EMAIL_TO_ACCOUNT = new HashMap<>();
        List<Element> toList = configTo.elements();
        for (Element toElement : toList) {
            EMAIL_TO_ACCOUNT.put(toElement.getText(), toElement.attributeValue("name"));
        }

        // ftp config
//        Element ftpConfig = config.element("ftpConfig");
//        FTP_URL = ftpConfig.elementText("url");
//        FTP_ACCOUNT = ftpConfig.elementText("account");
//        FTP_PASSWORD = ftpConfig.elementText("password");
//        FTP_PORT = Integer.parseInt(ftpConfig.elementText("port"));
//        FTP_OPEN = ftpConfig.elementText("open").equals("true");

        // socket
//        Element socketConfig = config.element("socketConfig");
//        SOCKET_OPEN = socketConfig.elementText("open").equals("true");
//        SOCKET_IP = socketConfig.elementText("ip");
//        SOCKET_PORT = socketConfig.elementText("prot");

        // Mail类初始化静态变量
        MAIL = new MyMail();
        MyMail.ENABLE_EMAIL = ENABLE_EMAIL;
        MyMail.EMAIL_HOST = EMAIL_HOST;
        MyMail.EMAIL_PORT = EMAIL_PORT;
        MyMail.EMAIL_FROM_ACCOUNT = EMAIL_FROM_ACCOUNT;
        MyMail.EMAIL_FROM_USERNAME = EMAIL_FROM_NAME;
        MyMail.EMAIL_FROM_PASSWORD = EMAIL_FROM_PASSWORD;
        // 单个备用接收账号（关闭邮件时使用）
        MyMail.TO_ACCOUNT = EMAIL_FROM_ACCOUNT;
        MyMail.TO_USERNAME = "Tooker";
        MyMail.TO_ACCOUNT_MAP.putAll(EMAIL_TO_ACCOUNT);
    }

    /**
     * 保存xml文件
     */
    public static void saveXml(){
        //指定文件输出的位置
        FileOutputStream out = null;
        try {
            out = new FileOutputStream(XML_PATH);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        // 指定文本的写出的格式：
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        //1.创建写出对象
        XMLWriter writer = null;
        try {
            writer = new XMLWriter(out, format);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            //2.写出Document对象
            writer.write(configDom);
            //3.关闭流
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
